<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'往来客户'),
	array('op'=>'edit','title'=>'添加往来客户'),
);
$return['statuss']=$this->kehu_status;
$return['kehu_jibie']=$this->kehu_jibie;
$return['kehu_type']=$this->kehu_type;
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_erp_kehu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'sn','title'=>'单位编号','type'=>'string'),
			array('name'=>'title','title'=>'单位名称','type'=>'string'),
			array('name'=>'groupid','title'=>'分管部门','type'=>'select','options'=>'groups'),
			array('name'=>'group2id','title'=>'分管职位','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'分管员工','type'=>'select','options'=>'workers'),
			array('name'=>'jibie','title'=>'单位级别','type'=>'radio','options'=>'kehu_jibie'),
			array('name'=>'type','title'=>'单位分类','type'=>'radio','options'=>'kehu_type'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'createtime','title'=>'时间','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'sn','width'=>15,'title'=>'单位编号','type'=>'string'),
		)
	);
	$return['workers']=pdo_getall('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$user_juese=$this->user_juese;
	$return['groups']=array();
	foreach($user_juese as $key=>$title){
		$return['groups'][$key]=array('id'=>$key,'title'=>$title);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'sn'=>trim($post['sn']),
			'title'=>trim($post['title']),
			'groupid'=>trim($post['groupid']),
			'group2id'=>trim($post['group2id']),
			'workerid'=>trim($post['workerid']),
			'jibie'=>trim($post['jibie']),
			'type'=>trim($post['type']),
			'name'=>trim($post['name']),
			'mobile'=>trim($post['mobile']),
			'status'=>trim($post['status']),
		);
		if(!$id){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			pdo_insert('jy_erp_kehu',$data);
		}else{
			pdo_update('jy_erp_kehu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_erp_kehu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'往来客户',
		'edit'=>array(
			array('name'=>'sn','title'=>'单位编号','type'=>'string'),
			array('name'=>'title','title'=>'单位名称','type'=>'string'),
			array('name'=>'groupid','title'=>'分管部门','type'=>'select','options'=>'groups'),
			array('name'=>'group2id','title'=>'分管职位','type'=>'select','options'=>'groups'),
			array('name'=>'workerid','title'=>'分管员工','type'=>'select','options'=>'workers'),
			array('name'=>'jibie','title'=>'单位级别','type'=>'radio','options'=>'kehu_jibie'),
			array('name'=>'type','title'=>'单位分类','type'=>'radio','options'=>'kehu_type'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
		)
	);
	$return['workers']=pdo_getall('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$user_juese=$this->user_juese;
	$return['groups']=array();
	foreach($user_juese as $key=>$title){
		$return['groups'][$key]=array('id'=>$key,'title'=>$title);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_erp_kehu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的用户！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_erp_kehu',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}